package com.dream.cloud.controller;


import com.dream.cloud.entities.Pay;
import com.dream.cloud.entities.PayDTO;
import com.dream.cloud.resp.ResultData;
import com.dream.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {

    @Resource
    PayService payService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/pay/getConfig")
    public String getConfig(@Value("${dream.cloud}") String config) {
        return config + " port:" + port;
    }




    @PostMapping("/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入，返回值：" + i);
    }

    @DeleteMapping("/pay/delete/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<String>  deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success("成功删除，返回值：" + i);
    }


    @PutMapping("/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改，返回值：" + i);
    }

    @GetMapping("/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) throws InterruptedException {

        System.out.println(port);


        Thread.sleep(62000);

        Pay byId = payService.getById(id);

        if (ObjectUtils.isEmpty(byId)) {
            throw new RuntimeException("查询不到该流水");
        }
        return ResultData.success(byId);
    }

    @GetMapping("/pay/getAll")
    @Operation(summary = "查询所有流水",description = "查询所有支付流水方法")
    public ResultData<List<Pay>> getAll() {

        return ResultData.success(payService.getAll());
    }
}
